package com.imooc.springboot.dubbo.nilinili.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.imooc.springboot.dubbo.nilinili.entity.bean.PageBean;
import com.imooc.springboot.dubbo.nilinili.entity.dto.PhotoDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.PhotoDetailDTO;
import com.imooc.springboot.dubbo.nilinili.res.CodeEnum;
import com.imooc.springboot.dubbo.nilinili.res.ResultFactory;
import com.imooc.springboot.dubbo.nilinili.service.CategoryService;
import com.imooc.springboot.dubbo.nilinili.service.PhotoService;
import com.imooc.springboot.dubbo.nilinili.service.UserService;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/photo")
@Validated
public class PhotoController {

    @Reference
    private PhotoService photoService;

    @Reference
    private CategoryService categoryService;

    @GetMapping("/selectPhotos/{id}/{pageNum}")
    public String selectPhotosByCategoryId(@PathVariable Integer id, @PathVariable Integer pageNum) {
        if(pageNum != -1){
            PageBean<PhotoDTO> photoDTOPageInfo = photoService.selectPhotoPagesByCategoryId(pageNum, 4, id);
            return ResultFactory.createResult(CodeEnum.SUCCESS,"查询图片成功",photoDTOPageInfo);
        }else{
            List<PhotoDTO> photoDTOInfo = photoService.selectPhotosByCategoryId(id);
            return ResultFactory.createResult(CodeEnum.SUCCESS,"查询图片成功",photoDTOInfo);
        }
    }

    @GetMapping("/getPhotoById/{id}")
    public String getPhotoById(@PathVariable Integer id) {
        PhotoDetailDTO photoDetailDTO = photoService.getPhotoById(id);
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询图片成功",photoDetailDTO);
    }

    @PostMapping("/getHomePhotos")
    public String getHomePhotos(@RequestBody List<String> list) {
        List<PageBean<PhotoDTO>> resultList=new ArrayList();
        for (String name : list) {
            Integer id = categoryService.selectIdByName(name);
            PageBean<PhotoDTO> photoDTOPageInfo = photoService.selectPhotoPagesByCategoryId(1, 4, id);
            resultList.add(photoDTOPageInfo);
        }
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询主页图片成功",resultList);
    }

    @GetMapping("/getAllPhotos")
    public String getAllPhotos(){
        List<PhotoDTO> photoDTOInfo = photoService.selectAllPhotos();
        return ResultFactory.createResult(CodeEnum.SUCCESS, "查询图片成功",photoDTOInfo);
    }

    @GetMapping("/getShufflingUrls")
    public String getShuffingUrls(){
        return photoService.getShuffingUrls();
    }
    @PostMapping("/updateApprovalCountsById")
    public String updateApprovalCountsById(@RequestParam Integer photoId,@RequestHeader("token") String token){
        return photoService.updateApprovalCounts(photoId,token);
    }

    /**
     * 查看作品
     * @param userId
     * @param pageNum
     * @return
     */
   @GetMapping("/getPhotosByUserId")
    public String getPhotosByUserId(@RequestParam("userid") String userId,@RequestParam("pageNum") int pageNum){
        return photoService.getPhotosByUserId(userId,pageNum);
    }

    /**
     * 上传作品
     * @param photoDetailDTO
     * @return
     */
    @PostMapping("/insertPhoto")
    public String insertPhoto(@RequestBody PhotoDetailDTO photoDetailDTO,@RequestHeader("token") String token){
        boolean b = photoService.insertPhoto(photoDetailDTO,token);
        if(b){
            return ResultFactory.createResult(CodeEnum.SUCCESS,"插入图片成功",null);
        }else{
            return ResultFactory.createResult(CodeEnum.FAIL,"插入图片失败",null);
        }
    }

    /**
     * 获得最新照片
     * @return
     */
    @GetMapping("/getNewPhoto")
    public String getNewPhoto(){
        List<PhotoDTO> newPhoto = photoService.getNewPhoto();
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询最新图片成功",newPhoto);
    }

    /**
     * 添加图片观看量
     * @return
     */
    @PostMapping("/addPhotoViewCount")
    public String addPhotoViewCount(@RequestParam("photoId") Integer photoId){
        boolean b = photoService.addPhotoViewCount(photoId);
        if(b){
            return ResultFactory.createResult(CodeEnum.SUCCESS,"添加图片观看量成功",null);
        }else{
            return ResultFactory.createResult(CodeEnum.FAIL,"添加图片观看量失败",null);
        }
    }


    /**
     * @param photoId
     * @return
     */
    @PostMapping("/deletePhoto")
    public String deletePhoto(@RequestParam("photoId") String photoId){
        return photoService.deletePhotoByPhotoId(photoId);
    }

    /**
     * 根据图片名字搜索图片
     * @param photoName
     * @return
     */
    @GetMapping("/getPhotoByName/{photoName}")
    public String getPhotoByName(@PathVariable("photoName") String photoName){
        List<PhotoDTO> photoByName = photoService.getPhotoByName(photoName);
        if (photoByName!=null){
            return ResultFactory.createResult(CodeEnum.SUCCESS,"查询图片成功",photoByName);
        }else{
            return ResultFactory.createResult(CodeEnum.FAIL,"查询不到该图片",null);
        }
    }
}
